// pages/change/change.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message:{},
    nameText:'',
    phoneText:'',
    qqnumText:'',
    array: ['男', '女', '秘密'],
    objectArray: [
      {
        id: 0,
        name: '男'
      },
      {
        id: 1,
        name: '女'
      },
      {
        id: 2,
        name: '秘密'
      }
    ],
    index: 1,
    date: '2021-11-26',
    region: ['河北省', '石家庄市', '裕华区'],
    customItem: '全部',
    tempFilePaths:wx.getStorageSync('users').user_image_src
  },


  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      'message.user_sex':parseInt(e.detail.value)+1,
    })
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      'message.user_birth':e.detail.value,
    })
  },
  
  bindRegionChange: function (e) {
    var _this=this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      'message.user_place':e.detail.value.join('-'),
      
    })
  },

//图片更改
  chooseimage:function(){
    var _this=this;
    wx.chooseImage({
      count:1,
      sizeType:['original','compressed'],
      sourceType:['album','camera'],
      success:function(res){
        _this.setData({
          'message.userImage':res.tempFilePaths[0],
          tempFilePaths:res.tempFilePaths[0]
        })
      }
    })
  },

//名字更改
  name:function(e){
    var that = this;
    that.setData({
      nameText:e.detail.value,
      'message.user_name':e.detail.value,
    })
  },
//性别更改
  sex:function(e){
    var that = this;
    that.setData({
      sexText:e.detail.value,
      
    })
  },
//手机号
  phone:function(e){
    var that = this;
    that.setData({
      phoneText:e.detail.value,
      'message.user_phone':e.detail.value,
    })
  },
  //qq
  qqnum:function(e){
    var that = this;
    that.setData({
      qqnumText:e.detail.value,
      'message.user_qq':e.detail.value,
    })
  },

  //保存
  save:function(){
    var _this = this;
    console.log(_this.data.message)
    wx.request({
      url: 'https://api.yidan.work:2004/information',
      header:{'Access-Control-Allow-Origin':'*'},
      header:{'Access-Control-Allow-Methods':'GET,POST'},
      method: 'POST',
      data:_this.data.message,
      success:(res)=>{
        console.log(res);
        wx.setStorageSync('users', _this.data.message)
      },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    wx.request({
      url: `https://api.yidan.work:2004/information/${wx.getStorageSync('user_id')}`,
      header:{'Access-Control-Allow-Origin':'*'},
      header:{'Access-Control-Allow-Methods':'GET,POST'},
      method: 'GET',
      success:(res)=>{
        console.log(res.data)
        _this.setData({
          message:res.data[0],
          date:res.data[0].user_birth,
          index:parseInt(res.data[0].user_sex)-1,
        })
        if(res.data[0].user_qq==null){
          _this.setData({'message.user_qq':'无'})
        }
        if(res.data[0].user_place&&res.data[0].user_place!='无'){
          var regions=_this.data.message.user_place.split('-')
          _this.setData({
            region:regions
          })
        } 
        
      },
      fail:(res)=>{
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})